home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / ascii.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  2KB  |  93 lines

  1. /* ascii - list lines with/without ASCII characters */
  2.  
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8.  
  9. #define BUFSIZE 4*1024
  10.  
  11. static char buf[BUFSIZE + 1];    /* input buffer - +1 for sentinel */
  12. static char carry[BUFSIZE];    /* buffer for partial line carryover */
  13.  
  14. int main(argc, argv)
  15. int argc;
  16. char **argv;
  17. {
  18.   int nflag = 0;        /* 1 if invoked with -n */
  19.   int ascii_line;        /* set to 1 if line is all ASCII */
  20.   int ascii_file = 1;        /* set to 0 if file is not all ASCII */
  21.   int count;            /* count of characters in buf */
  22.   char *start;            /* points to beginning of line */
  23.   register char *end;        /* points to end of line */
  24.   char *sentinel;        /* points past last character in buffer */
  25.   int carry_count;        /* size of carry over */
  26.  
  27.   --argc;
  28.   ++argv;
  29.   if (argc > 0 && strcmp(*argv, "-n") == 0) {
  30.     nflag = 1;
  31.     --argc;
  32.     ++argv;
  33.   }
  34.   switch (argc) {
  35.       case 0:
  36.     break;
  37.       case 1:
  38.     close(0);
  39.     if (open(*argv, O_RDONLY) != 0) {
  40.         std_err("ascii: cannot open ");
  41.         std_err(*argv);
  42.         std_err("\n");
  43.         exit(1);
  44.     }
  45.     break;
  46.       default:
  47.     std_err("Usage: ascii [-n] file\n");
  48.     exit(1);
  49.   }
  50.  
  51.   if ((count = read(0, buf, BUFSIZE)) <= 0) exit(0);
  52.   *(sentinel = &buf[count]) = '\n';
  53.   start = buf;
  54.   ascii_line = 1;
  55.   carry_count = 0;
  56.   while (1) {
  57.     for (end = start; *end != '\n'; ++end)
  58.         if ((*end & 0x80) != 0) {
  59.             ascii_line = 0;
  60.             ascii_file = 0;
  61.             end = (char *) memchr(end, '\n', BUFSIZE);
  62.             break;
  63.         }
  64.     if (end != sentinel) {
  65.         ++end;
  66.         if (ascii_line != nflag) {
  67.             if (carry_count != 0)
  68.                 fwrite(carry, carry_count, 1, stdout);
  69.             fwrite(start, (int)(end - start), 1, stdout);
  70.         }
  71.         carry_count = 0;
  72.         start = end;
  73.         ascii_line = 1;
  74.     } else {
  75.         if (carry_count != 0) {
  76.             std_err("ascii: line too long\n");
  77.             exit(1);
  78.         }
  79.         if (ascii_line != nflag) {
  80.             carry_count = end - start;
  81.             memcpy(carry, start, carry_count);
  82.         }
  83.         if ((count = read(0, buf, BUFSIZE)) <= 0) break;
  84.         *(sentinel = &buf[count]) = '\n';
  85.         start = buf;
  86.     }
  87.   }
  88.   if (ascii_line != nflag && carry_count != 0)
  89.     fwrite(carry, carry_count, 1, stdout);
  90.  
  91.   exit(ascii_file == 0);
  92. }
  93.